// subpackage/pages/relief/index.js
import requestUtil from '../../../baseRequest'
import requestUrls from '../../../requestUrl'
import imageUtils from '../../../utils/imageUtils'
import {
  $Message
} from '../../../components/base/index'
let openid = getApp().getUserOpenid()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    responsibility:[],
    medicalCertificate:[],
    showLoading:false,
    optType:'',
    orderId:'',
    cardCode:'',
  },
  afterRDSRead(event){
    const { file } = event.detail;
    let uploadUrl = requestUrls.baseUrl + requestUrls.orderTruckImage.uploadImage+"/ACCIDENT_CONFIRMATION";
    if(file.length>0){
      this.setData({showLoading: true});
      file.forEach((item,index)=>{
        let filePath = item.path;
        imageUtils.uploadImage(uploadUrl,filePath,(url)=>{
          this.setData({
            responsibility:[...this.data.responsibility,{url: requestUrls.baseImagePrefixUrl + url, imageUrl: url,imageTypeId:'ACCIDENT_CONFIRMATION'}],
            showLoading:!index===file.length
          })
        });
      })
    }
  },
  removeRDS(event){
    let imgList = this.data.responsibility;
    imgList.splice(event.detail.index,1);
    this.setData({
      responsibility: imgList,
    });
  },
  afterZDSRead(event){
    const { file } = event.detail;
    let uploadUrl = requestUrls.baseUrl + requestUrls.orderTruckImage.uploadImage+"/MEDICAL_CERTIFICATE";
    if(file.length>0){
      this.setData({showLoading: true});
      file.forEach((item,index)=>{
        let filePath = item.path;
        imageUtils.uploadImage(uploadUrl,filePath,(url)=>{
          this.setData({
            medicalCertificate:[...this.data.medicalCertificate,{url: requestUrls.baseImagePrefixUrl + url, imageUrl: url,imageTypeId:'MEDICAL_CERTIFICATE'}],
            showLoading:!index===file.length
          })
        });
      })
    }
  },
  removeZDS(event){
    let imgList = this.data.medicalCertificate;
    imgList.splice(event.detail.index,1);
    this.setData({
      medicalCertificate: imgList,
    });
  },
  applyRescue(value){
    const cardCode = value.detail.value.cardCode;
    if(!cardCode){
      this.showErrorMessage("请填写会员卡号");
      return false;
    }
    if(cardCode && cardCode !== this.data.cardCode){
      wx.setStorageSync('cardCode', cardCode);
    }
    const {responsibility,medicalCertificate} = this.data;
    if(responsibility.length===0){
      this.showErrorMessage("请上传交警认定书");
      return false;
    }
    if(medicalCertificate.length===0){
      this.showErrorMessage("请上传医生诊断书或医院证明");
      return false;
    }
    let formData ={
      openId: openid,
      criteria:{
        id:this.data.orderId,
        cardCode,
        imageList:responsibility.concat(medicalCertificate),
      }
    }
    this.setData({ showLoading: true });
    if(this.data.optType==='add'){
      requestUtil.postRequest(requestUrls.rescueApply.saveApplyOrder, { ...formData }, (res) => {
        this.setData({ showLoading: false });
        if (!res.hasError) {
          wx.showModal({
            title: '温馨提示',
            content: '提交成功',
            showCancel: false,
            success: res => {
              wx.switchTab({
                url: '/pages/home/index2'
                // url: '/pages/homev2/index'
              })
            }
          })
        }
      },(e)=>this.setData({ showLoading: false }))
    }else if(this.data.optType==='reEdit'){
      requestUtil.postRequest(requestUrls.rescueApply.reEditOrder, { ...formData }, (res) => {
        this.setData({ showLoading: false });
        if (!res.hasError) {
          wx.showModal({
            title: '温馨提示',
            content: '提交成功',
            showCancel: false,
            success: res => {
              wx.switchTab({
                url: '/pages/home/index2'
              })
            }
          })
        }
      },(e)=>this.setData({ showLoading: false }))
    }
  },
  getById(){
    const {orderId} = this.data;
    let formData = {
      openId:openid,
      criteria:{
        id:orderId,
      }
    };
    requestUtil.postRequest(requestUrls.rescueApply.getById, formData, (res) => {
      if(!res.hasError){
        let rescueApplyOrder = res.result;
        let imageList = rescueApplyOrder.imageList || [];
        let responsibility = [],medicalCertificate=[];
        if(imageList.length>0){
          imageList.forEach(image=>{
            if(image.imageTypeId==="ACCIDENT_CONFIRMATION"){
              responsibility.push({...image,url:requestUrls.baseImagePrefixUrl + image.imageUrl});
            }
            if(image.imageTypeId==="MEDICAL_CERTIFICATE"){
              medicalCertificate.push({...image,url:requestUrls.baseImagePrefixUrl + image.imageUrl});
            }
          })
        };
        this.setData({
          rescueApplyOrder,
          responsibility,
          medicalCertificate,
          cardCode:rescueApplyOrder.cardCode,
        });
      }
    })

  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {orderId} = options;
    //重新编辑医疗救助单
    if(orderId){
      this.setData({
        orderId,
        optType:'reEdit'
      })
      this.getById();
    }else{
      //新建医疗垫付单
      const cardCode = wx.getStorageSync('cardCode');
      if(cardCode){
        this.setData({
          cardCode
        })
      }
      this.setData({
        optType:'add'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})